const express = require('express');
const router = express.Router();
const auth = require('../middleware/auth');
const socketUtil = require('../utils/socketUtil');

// 获取用户在线状态 - 需要认证
router.get('/status/:userId', auth.verifyToken, async (req, res) => {
  try {
    const userId = req.params.userId;
    const userStatus = await socketUtil.getUserOnlineStatus(userId);
    
    res.status(200).json({
      success: true,
      userId: userId,
      isOnline: userStatus.is_online,
      status: userStatus.status,
      last_online: userStatus.last_online,
      timestamp: new Date().toISOString()
    });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取用户状态失败', error: error.message });
  }
});

// 获取所有在线用户列表 - 需要认证
router.get('/online-users', auth.verifyToken, async (req, res) => {
  try {
    const onlineUsers = socketUtil.getOnlineUsers();
    const totalCount = socketUtil.getOnlineCount();
    
    res.status(200).json({
      success: true,
      totalCount: totalCount,
      onlineUsers: onlineUsers,
      timestamp: new Date().toISOString()
    });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取在线用户列表失败', error: error.message });
  }
});

module.exports = router;